package com.test.deencredit.view_deencredit.process_widget_deencredit;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.test.deencredit.R;
import com.test.deencredit.adapter_deencredit.ProcessInfoInputListAdapter_deencredit;
import com.test.deencredit.base_deencredit.BaseProcessWidgetManager_deencredit;
import com.test.deencredit.bean_deencredit.process_deencredit.ProcessInfo_Data_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.util.ArrayList;
import java.util.Arrays;

public class SelectViewManager_deencredit extends BaseProcessWidgetManager_deencredit {

    private TextView titleTv_deencredit;
    private TextView inputTv_deencredit;
    private View listLayout_deencredit;
    private ListView listLv_deencredit;

    private int itemHeight_deencredit = 0;
    private ArrayList<String> items_deencredit;

    public SelectViewManager_deencredit(View view, ProcessInfo_Data_deencredit data, String page){
        super(view,data,page);
    }

    @Override
    protected void init_deencredit(){

        titleTv_deencredit = mView_deencredit.findViewById(R.id.vps_tv_title);
        View clickLayout = mView_deencredit.findViewById(R.id.vps_view_click);
        inputTv_deencredit = mView_deencredit.findViewById(R.id.vps_tv_input);
        listLayout_deencredit = mView_deencredit.findViewById(R.id.vps_view_list);
        listLv_deencredit = mView_deencredit.findViewById(R.id.vps_lv_list);

        setTitle_deencredit(mData_deencredit.getTitle_deencredit());
        inputTv_deencredit.setHint(mData_deencredit.getHint_deencredit());

        clickLayout.setOnClickListener(v -> {
            listLayout_deencredit.setVisibility(listLayout_deencredit.getVisibility()==VISIBLE ? GONE:VISIBLE);
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit, mData_deencredit.getParamName_deencredit().toUpperCase()+"_CLICK","");
        });

        addItems_deencredit(mData_deencredit.getTitlesOfValue_deencredit());
    }

    @Override
    public boolean checkInput_deencredit() {
        if (!mData_deencredit.isMustInput_deencredit()) return true;
        return !TextUtils.isEmpty(inputTv_deencredit.getText());
    }

    @Override
    public String getInput_deencredit() {
        if (mData_deencredit == null) return "";
        return mData_deencredit.getValueOfTitle_deencredit(inputTv_deencredit.getText().toString());
    }

    public String getTextInput_deencredit(){
        return inputTv_deencredit.getText().toString();
    }

    public void setTitle_deencredit(String title){
        if (TextUtils.isEmpty(title)) titleTv_deencredit.setVisibility(GONE);
        else {
            titleTv_deencredit.setVisibility(VISIBLE);
            titleTv_deencredit.setText(title);
        }
    }

    public void addItems_deencredit(ArrayList<String> itemList){
        items_deencredit = itemList;
        ProcessInfoInputListAdapter_deencredit listAdapter = new ProcessInfoInputListAdapter_deencredit(mView_deencredit.getContext(), items_deencredit);
        listLv_deencredit.setAdapter(listAdapter);
        listLv_deencredit.setOnItemClickListener((parent, view, position, id) -> {
            if (items_deencredit ==null) return;
            String input = items_deencredit.get(position);

            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit, mData_deencredit.getParamName_deencredit().toUpperCase()+"_CLICK",input);

            inputTv_deencredit.setText(input);
            listLayout_deencredit.setVisibility(GONE);
        });

        listAdapter.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = listLv_deencredit.getLayoutParams();
        if (itemHeight_deencredit ==0) itemHeight_deencredit = layoutParams.height;
        layoutParams.height = items_deencredit.size()* itemHeight_deencredit;
        listLv_deencredit.setLayoutParams(layoutParams);

    }

    public void addItems_deencredit(String[] itemArr){
        addItems_deencredit(new ArrayList<>(Arrays.asList(itemArr)));
    }

}
